package com.zhang.novel.controller;

import com.github.pagehelper.PageInfo;
import com.zhang.novel.core.bean.ResultBean;
import com.zhang.novel.core.enums.ResponseStatus;
import com.zhang.novel.entity.Author;
import com.zhang.novel.entity.Book;
import com.zhang.novel.service.AuthorService;
import com.zhang.novel.service.BookService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@Api(tags = "作者接口")
@RequiredArgsConstructor
@RequestMapping("author")
@Slf4j
public class AuthorController extends BaseController {

    private final AuthorService authorService;

    private final BookService bookService;

    /**
     * 校验笔名是否存在
     *
     * @param penName
     * @return
     */
    @PostMapping("checkPenName")
    public ResultBean checkPenName(String penName) {
        return ResultBean.ok(authorService.checkPenName(penName));
    }

    /**
     * 作者发布小说的分页查询
     *
     * @param page
     * @param pageSize
     * @param request
     * @return
     */
    @PostMapping("listBookByPage")
    public ResultBean listBookByPage(@RequestParam(value = "curr", defaultValue = "1") int page,
                                     @RequestParam(value = "limit", defaultValue = "10") int pageSize,
                                     HttpServletRequest request) {
        return ResultBean.ok(new PageInfo<>(bookService.listBookPageByUserId(getUserDetails(request).getId(), page, pageSize)));
    }

    /**
     * 发布小说
     *
     * @param bookDesc
     * @param book
     * @param request
     * @return
     */
    public ResultBean addBook(@RequestParam("bookDesc") String bookDesc, Book book, HttpServletRequest request) {
        // 查询作家信息
        Author author = authorService.queryAuthor(getUserDetails(request).getId());

        // 判断作者状态是否正常
        if (1 == author.getStatus()) {
            // 封禁状态，不能发布小说
            return ResultBean.fail(ResponseStatus.AUTHOR_STATUS_FORBIDDEN);
        }

        // bookDesc不能使用book对象接收，否则会自动去掉前面的空格
        book.setBookDesc(bookDesc
                .replaceAll("\\n","<br>")
                .replaceAll("\\s","&nbsp;"));

        // 发布小说
        bookService.addBook(book,author.getId(),author.getPenName());
        return ResultBean.ok();
    }

    /**
     * 更新小说的状态，上架或下架
     *
     * @param bookId
     * @param status
     * @param request
     * @return
     */
    public ResultBean updateBookStatus(Long bookId, Byte status, HttpServletRequest request) {
        // 查询作家信息
        Author author = authorService.queryAuthor(getUserDetails(request).getId());

        // 判断作者状态是否正常
        if (1 == author.getStatus()) {
            // 封禁状态，不能发布小说
            return ResultBean.fail(ResponseStatus.AUTHOR_STATUS_FORBIDDEN);
        }

        // 更新小说状态，上架或下架
        bookService.updateBookStatus(bookId,status,author.getId());
        return ResultBean.ok();
    }

    /**
     * 发布章节内容
     *
     * @param bookId
     * @param indexName
     * @param content
     * @param request
     * @return
     */
    public ResultBean addBookContent(Long bookId, String indexName, String content, HttpServletRequest request) {
        // 查询作家信息
        Author author = authorService.queryAuthor(getUserDetails(request).getId());

        // 判断作者状态是否正常
        if (1 == author.getStatus()) {
            // 封禁状态，不能发布小说
            return ResultBean.fail(ResponseStatus.AUTHOR_STATUS_FORBIDDEN);
        }
        content=content.replaceAll("\\n","<br>")
                .replaceAll("\\s","&nbsp;");

        // 发布章节内容
        bookService.addBookContent(bookId,indexName,content,author.getId());
        return ResultBean.ok();
    }
}
